import { _decorator, Camera, Component, Node, Size, UITransform, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MapUI')
export class MapUI extends Component {

    @property(Camera)
    camera: Camera = null; // 正交相机

    @property(Node)
    miniMapNode: Node = null; // 小地图UI节点（Sprite节点）

    @property(Node)
    poisonCircleNode: Node = null; // 毒圈UI节点

    @property(Node)
    nextCircleNode: Node = null; // 下一圈UI节点

    @property(Node)
    poison: Node;

    @property(Node)
    next: Node;

    // 毒圈世界坐标和半径（示例值，实际从游戏逻辑获取）
    private worldPos: Vec3 = new Vec3(0, 0, 0);
    private worldRadius: number = 10;

    start() {

    }

    update(deltaTime: number) {
        this.updatePoisonCircle();
    }

    private updatePoisonCircle() {
        if (!this.camera || !this.miniMapNode || !this.poisonCircleNode) {
            return;
        }

        // 获取RenderTexture的尺寸
        const rt = this.camera.targetTexture;
        if (!rt) {
            return;
        }
        const rtWidth = rt.width;
        const rtHeight = rt.height;

        this.worldPos = this.poison.getWorldPosition();
        this.worldRadius = this.poison.worldScale.x;

        // 将世界坐标转换为RenderTexture上的像素坐标
        const screenPos = this.camera.worldToScreen(this.worldPos);
        // 计算世界半径对应的像素坐标
        const worldPos2 = new Vec3(this.worldPos.x + this.worldRadius, this.worldPos.y, this.worldPos.z);
        const screenPos2 = this.camera.worldToScreen(worldPos2);
        const pixelRadius = Vec2.distance(new Vec2(screenPos.x, screenPos.y), new Vec2(screenPos2.x, screenPos2.y));

        // 计算归一化UV坐标
        const u = screenPos.x / rtWidth;
        const v = screenPos.y / rtHeight;

        // 获取小地图UI节点的尺寸
        const uiTransform = this.miniMapNode.getComponent(UITransform);
        const uiWidth = uiTransform.contentSize.width;
        const uiHeight = uiTransform.contentSize.height;

        // 计算毒圈中心在小地图UI上的局部坐标
        const localX = u * uiWidth - uiWidth / 2;
        const localY = v * uiHeight - uiHeight / 2;

        // 计算毒圈在小地图UI上的半径
        const uiRadius = pixelRadius * (uiWidth / rtWidth);

        // 设置毒圈节点的位置和大小
        this.poisonCircleNode.setPosition(localX, localY);
        const poisonUITransform = this.poisonCircleNode.getComponent(UITransform);
        poisonUITransform.contentSize = new Size(uiRadius, uiRadius);

        this.worldPos = this.poison.getWorldPosition();
        this.worldRadius = this.poison.worldScale.x;



        this.worldPos = this.next.getWorldPosition();
        this.worldRadius = this.next.worldScale.x;

        // 将世界坐标转换为RenderTexture上的像素坐标
        const screenPos3 = this.camera.worldToScreen(this.worldPos);
        // 计算世界半径对应的像素坐标
        const worldPos4 = new Vec3(this.worldPos.x + this.worldRadius, this.worldPos.y, this.worldPos.z);
        const screenPos4 = this.camera.worldToScreen(worldPos4);
        const pixelRadius2 = Vec2.distance(new Vec2(screenPos3.x, screenPos3.y), new Vec2(screenPos4.x, screenPos4.y));

        // 计算归一化UV坐标
        const u2 = screenPos3.x / rtWidth;
        const v2 = screenPos3.y / rtHeight;

        // 获取小地图UI节点的尺寸
        const uiTransform2 = this.miniMapNode.getComponent(UITransform);
        const uiWidth2 = uiTransform2.contentSize.width;
        const uiHeight2 = uiTransform2.contentSize.height;

        // 计算毒圈中心在小地图UI上的局部坐标
        const localX2 = u2 * uiWidth2 - uiWidth2 / 2;
        const localY2 = v2 * uiHeight2 - uiHeight2 / 2;

        // 计算毒圈在小地图UI上的半径
        const uiRadius2 = pixelRadius2 * (uiWidth2 / rtWidth);

        // 设置毒圈节点的位置和大小
        this.nextCircleNode.setPosition(localX2, localY2);
        const poisonUITransform2 = this.nextCircleNode.getComponent(UITransform);
        poisonUITransform2.contentSize = new Size(uiRadius2, uiRadius2);
    }

    protected onEnable(): void {
        this.camera.enabled = true;
    }

    protected onDisable(): void {
        this.camera.enabled = false;
    }
}


